import time

import requests
from lxml import html, etree

url = 'https://m.ctrip.com/restapi/soa2/19728/fuzzySearch?_fxpcqlniredt=09031030115939439964&x-traceID=09031030115939439964-1715936959737-563088'
res = requests.post(url=url, json={
    "tt": 1,
    "source": "online_theme",
    "st": 6,
    "segments": [
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "15"
            ]
        },
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "13"
            ]
        },
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "12"
            ]
        },
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "21"
            ]
        },
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "19"
            ]
        },
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "22"
            ]
        },
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "16"
            ]
        },
        {
            "dcl": [
                "CGO"
            ],
            "acl": [
                "17"
            ]
        }
    ],
    "head": {
        "cid": "09031030115939439964",
        "ctok": "",
        "cver": "1.0",
        "lang": "01",
        "sid": "8888",
        "syscode": "999",
        "auth": "",
        "xsid": "",
        "extension": []
    }
})
for msg in res.json()['routes']:
    # print(msg)
    for message in msg['pl']:
        price = message['price']
    # print(msg['departCity']['name'], msg['arriveCity']['name'], msg['arriveCity']['imageUrl'], price)
    img_res = requests.get(msg['arriveCity']['imageUrl'])
    with open(f"../datas/携程旅游/{msg['departCity']['name']}-{msg['arriveCity']['name']}.jpg", "wb") as f:
        f.write(img_res.content)
    print(f"{msg['departCity']['name']}-{msg['arriveCity']['name']}爬取完毕")
    time.sleep(1)
